//
//  CustomTabBarController.m
//  MyMovie
//
//  Created by apple on 16/5/10.
//  Copyright © 2016年 ljp. All rights reserved.
//

#import "CustomTabBarController.h"
#import "MainTabBarButton.h"

@interface CustomTabBarController ()

{
    UIImageView *_selectedImageView;
}
@end

@implementation CustomTabBarController


- (instancetype)init
{
    self = [super init];
    if (self) {

        [self loadSubViewController];
        [self customTabBarButton];
    }
    return self;
}

- (void)loadSubViewController {



    NSMutableArray *ncArray = [[NSMutableArray alloc] init];

    NSArray *fileName = @[@"MovieStoryboard",
                          @"NewsStoryboard",
                          @"TopStoryboard",
                          @"CinemaStoryboard",
                          @"MoreStoryboard"];
    for (NSString *sbName in fileName) {


        UIStoryboard *sb = [UIStoryboard storyboardWithName:sbName bundle:[NSBundle mainBundle]];
        //获取故事版第一个控制器
        UINavigationController *nc = [sb instantiateInitialViewController];
        //通过识别符获取指定控制器
        //instantiateViewControllerWithIdentifier:(NSString *)identifier;
        [ncArray addObject:nc];
    }

    self.viewControllers = ncArray;
}
- (void)customTabBarButton {

    self.tabBar.backgroundImage = [UIImage imageNamed:@"tab_bg_all"];

    for (UIView *button in self.tabBar.subviews) {
        if ([button isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [button removeFromSuperview];
        }
    }

    NSArray *titleArray = @[@"电影",
                            @"新闻",
                            @"top",
                            @"影院",
                            @"更多"];
    NSArray *imageNameArray = @[@"movie_home",
                                @"msg_new",
                                @"start_top250",
                                @"icon_cinema",
                                @"more_select_setting"];

    CGFloat buttonWidth = [UIScreen mainScreen].bounds.size.width/titleArray.count;

    for (int i = 0; i < titleArray.count ; i++) {

        MainTabBarButton *button = [MainTabBarButton mainTabBarButtonWithImage:[UIImage imageNamed:imageNameArray[i]] Title:titleArray[i] Frame:CGRectMake(i*buttonWidth, 0, buttonWidth, 49)];

        button.tag = i;

        [button addTarget:self action:@selector(tabBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];

        [self.tabBar addSubview:button];

    }
    _selectedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, buttonWidth, 49)];
    _selectedImageView.image = [UIImage imageNamed:@"selectTabbar_bg_all1"];
    _selectedImageView.alpha = 0.5;

    [self.tabBar addSubview:_selectedImageView];
}

- (void)tabBarButtonAction:(MainTabBarButton *)button {

    self.selectedIndex = button.tag;
    [UIView animateWithDuration:0.3 animations:^{

        _selectedImageView.center = button.center;
    }];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}



@end
